#!/bin/sh
#
#  This script sets up the /etc/network/interface file for the new
# image.
#
# Steve
# --
# http://www.steve.org.uk/


prefix=$1

#
#  Source our common functions
#
if [ -e /usr/lib/xen-tools/common.sh ]; then
    . /usr/lib/xen-tools/common.sh
else
    . ./hooks/common.sh
fi


#
# Log our start
#
logMessage Script $0 starting


#
#  Make sure we have an /etc/network directory.
#
mkdir -p ${prefix}/etc/network


#
#  A function to setup DHCP for our new image.
#
setupDynamicNetworking ()
{
    #
    # The host is using DHCP.
    #
    cat <<E_O_DHCP > ${prefix}/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp
post-up ethtool -K eth0 tx off

#
# The commented out line above will disable TCP checksumming which
# might resolve problems for some users.  It is disabled by default
#
E_O_DHCP
}



#
#  A function to setup static IP addresses for our new image.
#
setupStaticNetworking ()
{
    #
    # broadcast address?
    #
    bcast='';
    if [ ! -z "${broadcast}" ]; then
      bcast=" broadcast ${broadcast}"
    fi

    #
    # We have a static IP address
    #
    cat <<E_O_STATIC >${prefix}/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
 address ${ip1}
 gateway ${gateway}
 netmask ${netmask}
 ${bcast}
 post-up  ethtool -K eth0 tx off

#
# The commented out line above will disable TCP checksumming which
# might resolve problems for some users.  It is disabled by default
#
E_O_STATIC

    interface=1
    count=2

    while [ "${count}" -le "${ip_count}" ]; do

        value=\$ip${count}
        value=`eval echo $value`

        logMessage Adding etho:${interface}

        cat <<E_O_STATIC >>${prefix}/etc/network/interfaces
auto eth0:${interface}
iface eth0:${interface} inet static
 address ${value}
 netmask ${netmask}
 post-up  ethtool -K eth0 tx off
E_O_STATIC
        count=`expr $count + 1`
        interface=`expr $interface + 1`
    done
}




#
#  Call the relevant function
#
if [ -z "${dhcp}" ]; then
    logMessage "Setting up static networking"
    setupStaticNetworking

else
    logMessage "Setting up DHCP networking"
    setupDynamicNetworking
fi


#
#  Log our finish
#
logMessage Script $0 finished
